<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
require_once APP . '/application/models/ArticleModel.php';
require_once APP.'/application/models/Alllist.php';

/**
 * 新闻管理
 * @package admin
 */
class NewsController extends AdminAction
{
    

    public function init()
    {
    }


    /**
     * 加载框架页
     */
    public function addAction()
    {
    	global $cfg;
    	
    	$aid = (int)$this->getParam("aid");
    	$cid = $this->getParam("cid");    	
    	$nowp = (int) $this->getParam("nowp");
    	
    	$title = "添加";
    	$content = array();
    	if (!empty($aid)){
    		$title = "编辑";
    		$obj_article = new ArticleModel($this->db);
    		$content = $obj_article->show($aid);
    	}
    	
    	$list = new Alllist($this->db);
    	$arr_xwzx = $list->getAllListByType($cfg['listtype']['xwzx']);
    	$sync = false;
    	$recommend = false;
    	$image = true;
    	$intro = true;
    	$imageintro = "";
    	if ($cid == 1){// 新闻聚焦，同步
    		$sync = true;
    		$recommend = true;
			$imageintro = "图片尺寸：200*145";
    	}elseif ($cid == 2){// 媒体报道
    		$arr_type = $list->getAllListByType($cfg['listtype']['mtbd']);
    		$this->view->arr_type = $arr_type;
    		$recommend = false;
    		
			$imageintro = "图片尺寸：200*145";
    	}elseif ($cid == $cfg['listtype']['tzgg']){
    		$image = false;
    		$intro = false;
    	}elseif ($cid== $cfg['listtype']['tzgg']){
    		$intro = true;
    		$image = true;
    	}elseif ($cid== $cfg['listtype']['tpxw']){
			$imageintro = "图片尺寸：510*340";
    	}
    	
    	// 新闻中心
    	if (isset($arr_xwzx[$cid])){    		
			$imageintro = "图片尺寸：200*145";
    	}
    	
    	// 编辑状态
    	if (!empty($aid)){
    		$sync = false;
    	}
    	
    	
    	$this->view->imageintro = $imageintro;
    	$this->view->recommend = $recommend;
    	
    	$this->view->image = $image;
    	$this->view->intro = $intro;
    	$this->view->title = $title;
        $this->view->content = $content;
    	$this->view->sync = $sync;
    	
        $this->view->cid = $cid;
        $this->view->aid = $aid;
        $this->view->nowp = $nowp;
    }
	
    
    public function doaddAction()
    {
    	$param = $this->getAllParams();
    	$cid = (int) $param['cid'];
    	$aid = (int) $param['aid'];
    	
    	if (!empty($aid)){
    		$this->doeditAction();
    		return;
    	}
    	
    	if (empty($param['title'])){
    		$url = $this->url($this->view->baseUrl, "news", "add", "cid=" . $cid, "admin");
	    	$this->showmessage($url, '标题不能为空');
	    	return;
    	}
    	
    	// 置顶处理
    	if ($this->getParam("top") == 1){
    		$param['seq'] = $this->db->fetchOne("select max(seq)+1 from article where cid=" . $cid);
    	}
    	
    	$image = Util::upfile("image", "image");
    	!empty($image) && $param['image'] = $image;
    	
    	$pdf = Util::upfile("pdf", "pdf");
    	!empty($pdf) && $param['pdf'] = $pdf;
    	
    	$obj_article = new ArticleModel($this->db);
    	$flag_success = false;
    	try{
    		$aid = $obj_article->add($param);
    		$this->view->msg = '添加成功';
    		$flag_success = true;
    	}catch (Exception $e){
    		$tihs->view->msg = "添加失败！" . $e->getMessage();
    	}
    	
    	// 同步处理
    	$params = $param;
    	if (!empty($params['sync_cb']) && $flag_success){
    		global $cfg;
    		    		
    		unset($params['m'], $params['c'], $params['a'], $params['__url__'], 
    		$params['module'], $params['controller'], $params['action']);
    		
    		$params['do'] = 'add';
    		
    		$curl = curl_init($cfg['syncurl'] . '&a=xwrd');
			curl_setopt($curl,CURLOPT_POST,true);
			curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl,CURLOPT_POSTFIELDS,$params);
			$data = curl_exec($curl);
			curl_close($curl);
			$result = json_decode($data, true);
			$syncaid = (int)$result['aid'];
			if ($syncaid > 0){// 修改同步的pid
				$this->db->update("article", array("syncaid" => $syncaid), "aid=" . $aid);
			}
    	}
    	
    	$this->view->url = $this->url($this->view->baseUrl, "news", "add", "cid=" . $cid, "admin");
    	$this->showmessage();
    }
    
    /**
     * 编辑
     *
     */
    public function doeditAction()
    {
    	$param = $this->getAllParams();
    	$cid = (int) $param['cid'];
    	$aid = (int) $param['aid'];
    	
    	if (!isset($param['recommend']))
    		$param['recommend'] = 0;
    	if (!isset($param['btnpdf']))
    		$param['btnpdf'] = 0;
    	
    	if (empty($param['title'])){
    		$url = $this->url($this->view->baseUrl, "news", "add", "cid=" . $cid, "admin");
	    	$this->showmessage($url, '标题不能为空');
	    	return;
    	}
    	
    	// 置顶处理
    	if ($this->getParam("top") == 1){
    		$param['seq'] = $this->db->fetchOne("select max(seq)+1 from article where cid=" . $cid);
    	}
    	
    	$image = Util::upfile("image", "image");
    	!empty($image) && $param['image'] = $image;
    	
    	$pdf = Util::upfile("pdf", "pdf");
    	!empty($pdf) && $param['pdf'] = $pdf;
    	
    	// 删除
    	if (!empty($param['delimage']))
    		$param['image'] = "";
    	
    	if (!empty($param['delpdf']))
    		$param['pdf'] = "";
    	
    	
    	$obj_article = new ArticleModel($this->db);
    	$flag_success = false;
    	try{
    		$aid = $obj_article->change($aid, $param);
    		$msg = '编辑成功';
    		$flag_success = true;
    	}catch (Exception $e){
    		$msg = "编辑失败！" . $e->getMessage();
    	}
    	
    	$content = $obj_article->show($aid);
    	// 同步处理
    	$params = $param;
    	if (!empty($content['syncaid']) && $flag_success){
    		global $cfg;
    		
    		unset($params['m'], $params['c'], $params['a'], $params['__url__'], 
    		$params['module'], $params['controller'], $params['action']);
    		
    		$params['do'] = 'change';
    		$params['aid'] = $content['syncaid'];
    		
    		$curl = curl_init($cfg['syncurl'] . '&a=xwrd');
			curl_setopt($curl,CURLOPT_POST,true);
			curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl,CURLOPT_POSTFIELDS,$params);
			$data = curl_exec($curl);
			curl_close($curl);
    	}
    	
    	$url = $this->url($this->view->baseUrl, "news", "list", "nowp=" . $param['nowp'] . '&cid=' . $cid, "admin");
    	$this->showmessage($url, $msg);
    }
    
    
    public function listAction()
    {
    	$cid = (int) $this->getParam("cid");
    	$nowp = (int) $this->getParam("nowp", 1);
    	$query = trim($this->getParam("query", ""));
    	
    	$per_page = 20;
		$where = "where cid=$cid";
		
		if (!empty($query)){
			$query = mysql_escape_string($query);
			$where .= " and (title like '%$query%' or content like '%$query%' or keywords like '%$query%' or author like'%$query%')";
		}
		
		$obj_article = new ArticleModel($this->db);
		$result = $obj_article->fetchAll($cid, $nowp, $per_page, $where);
    	
    	$this->view->cid = $cid;
    	$this->view->nowp = $nowp;
        $this->view->list = $result['list'];
        $this->view->page = $result['page'];
        $this->view->pagenum = Util::pageNumStyle($result['page']);
        
        $query = stripslashes($query);        	
        $this->view->query = $query;
    }
    
    
    /**
     * 删除
     *
     */
    public function delAction()
    {
    	$aid = (int) $this->getParam("aid");
    	$cid = (int)$this->getParam("cid");
    	$nowp = (int) $this->getParam("nowp");
    	$query = $this->getParam("query");
    	
    	$obj_article = new ArticleModel($this->db);
    	$content = $obj_article->show($aid);
    	$obj_article->del($aid);
    	
    	$url = $this->url($this->view->baseUrl, "news", "list", "nowp=" . $nowp . '&cid=' . $cid, "admin");
    	
    	if (!empty($query)){
    		$url .= '&query=' . urlencode($query);
    	}
    	
    	// 同步处理
    	if (!empty($content['syncaid'])){
    		global $cfg;
    		
    		$params = array();
    		$params['do'] = 'del';
    		$params['aid'] = $content['syncaid'];
    		$params['title'] = $content['title'];
    		
    		$curl = curl_init($cfg['syncurl'] . '&a=xwrd');
			curl_setopt($curl,CURLOPT_POST,true);
			curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl,CURLOPT_POSTFIELDS,$params);
			$data = curl_exec($curl);
			curl_close($curl);
    	}
    	
    	header("location:" . $url);
    }
}

function secho($obj, $key, $def="")
{
	if (isset($obj['content']) && isset($obj['content'][$key])){
		if ($key == 'pubtime')
			$obj['content'][$key] = substr($obj['content'][$key], 0, 10);
			
		echo htmlspecialchars($obj['content'][$key]);
	}else{
		if (!empty($def))
			echo $def;
	}
}